package com.jagochan.module.rbac.sys_role.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jagochan.common.result.ResultUtils;
import com.jagochan.common.result.ResultVo;
import com.jagochan.module.rbac.sys_role.domain.SysRole;
import com.jagochan.module.rbac.sys_role.domain.query.RolePageQuery;
import com.jagochan.module.rbac.sys_role.domain.req.RoleAssignMenuReq;
import com.jagochan.module.rbac.sys_role.domain.vo.SelectRoleOptionVO;
import com.jagochan.module.rbac.sys_role.service.SysRoleService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@RequiredArgsConstructor
@RequestMapping("/api/role")
@RestController
public class SysRoleController {

    private final SysRoleService sysRoleService;

    @SaCheckPermission("sys:role:add")
    @PostMapping
    public ResultVo<?> add(@RequestBody SysRole sysRole) {
        Date now = new Date();
        sysRole.setCreateTime(now);
        sysRole.setUpdateTime(now);
        if (sysRoleService.save(sysRole)) {
            return ResultUtils.success("新增成功!");
        }
        return ResultUtils.error("新增失败!");
    }

    @SaCheckPermission("sys:role:edit")
    @PutMapping
    public ResultVo<?> edit(@RequestBody SysRole sysRole) {
        sysRole.setUpdateTime(new Date());
        if (sysRoleService.updateById(sysRole)) {
            return ResultUtils.success("编辑成功!");
        }
        return ResultUtils.error("编辑失败!");
    }

    @SaCheckPermission("sys:role:delete")
    @DeleteMapping("{roleId}")
    public ResultVo<?> delete(@PathVariable("roleId") Long roleId) {
        if (sysRoleService.removeById(roleId)) {
            return ResultUtils.success("删除成功!");
        }
        return ResultUtils.error("删除失败!");
    }

    @GetMapping("/page")
    public ResultVo<Page<SysRole>> getList(RolePageQuery query) {
        // 构造分页对象
        Page<SysRole> page = Page.of(query.getCurrentPage(), query.getPageSize());
        // 查询条件
        String roleName = query.getRoleName();
        page = sysRoleService.lambdaQuery().like(StringUtils.isNotEmpty(roleName), SysRole::getRoleName, roleName)
                .orderByDesc(SysRole::getRoleId)
                .page(page);

        return ResultUtils.success("查询成功", page);
    }

    @GetMapping("/list-role-option")
    public ResultVo<List<SelectRoleOptionVO>> selectList() {
        List<SysRole> list = sysRoleService.list();

        List<SelectRoleOptionVO> SelectRoleOptionVOList = new ArrayList<>();
        Optional.ofNullable(list).orElse(new ArrayList<>())
                .forEach(item -> SelectRoleOptionVOList.add(SelectRoleOptionVO.builder()
                        .check(false).label(item.getRoleName()).value(item.getRoleId())
                        .build()));
        return ResultUtils.success("查询成功", SelectRoleOptionVOList);
    }

    /**
     * 给角色分配菜单
     */
    @SaCheckPermission("sys:role:assign")
    @PostMapping("/assign-menu")
    public ResultVo<?> assignMenuForRole(@RequestBody RoleAssignMenuReq roleAssignMenuReq) {
        sysRoleService.assignMenuForRole(roleAssignMenuReq.getRoleId(), roleAssignMenuReq.getMenuList());
        return ResultUtils.success("分配成功!");
    }

}